﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace SmartSpace.News 
{
    public class AssetDbContext : DbContext
    {
        public AssetDbContext()
            : base("name=smartspace")
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<AssetDbContext>(new MigrateDatabaseToLatestVersion<AssetDbContext, ADBC>());
            }
            else
            {
                Database.SetInitializer<AssetDbContext>(null);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Asset>().ToTable("assets_asset");
            modelBuilder.Entity<Asset>().HasKey(p => p.Id);

            modelBuilder.Entity<AssetCategory>().ToTable("assets_category");
            modelBuilder.Entity<AssetCategory>().HasKey(p => p.Id);

            base.OnModelCreating(modelBuilder);
        }
    }

    internal sealed class ADBC : DbMigrationsConfiguration<AssetDbContext>
    {
        public ADBC()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}
